//
//  LicensePlateViewController.m
//  NiuNiuPacking
//
//  Created by shaoting on 14-9-4.
//  Copyright (c) 2014年 Reseek. All rights reserved.
//

#import "LicensePlateViewController.h"
#import "NSString+SBJSON.h"
#import "ASIFormDataRequest.h"
#import "Toast+UIView.h"

@implementation LicensePlateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    self.txtLP.text=delegate.currentUser.carNumber;
    
}

- (void)dealloc {
    [_txtLP release];
    [super dealloc];
}
- (IBAction)Onclick:(id)sender {
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    
    //请求url
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.niuniuparking.com/api/v1/p_update_car_number?token=%@&car_number=%@",delegate.currentUser.token,self.txtLP.text]];
    
    //创建请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [request setHTTPMethod:@"POST"];//设置请求方式POST
   // NSString *str = [NSString stringWithFormat:@"&phone=%@&password=%@&platform=2",self.txtPhone.text,self.txtPassword.text];//内容参数
    NSString *str =@"";
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    
    //连接服务器
    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    NSString *str1 = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
    NSDictionary *jsonDict = [str1 JSONValue];
    int errnum = [jsonDict[@"errno"] intValue];
    if(errnum == 0) {
        
        delegate.currentUser.carNumber=self.txtLP.text;
        
        self.userBl = [[UserBl alloc] init];
        
        [self.userBl update:delegate.currentUser];
        [self.view makeToast:@"更新成功！"
                    duration:1.0
                    position:@"bottom"];
    }
    
    else
    {
        [self.view makeToast:jsonDict[@"errmsg"]
                    duration:1.0
                    position:@"bottom"];
    }

}
@end
